package com.swq.java.code.chapter15.code02;

import java.util.*;

public class ConcurrentTester {
    public static void main(String args[]) {
        final int size = 1000;
        final Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < size; i++)
            set.add(new Integer(i));

        Thread reader = new Thread() {  //遍历集合
            public void run() {
                // 准确说异常是在迭代器中抛出的，不是打印的时候抛出
                for (Integer i : set) {
                    System.out.println(i);  //抛出ConcurrentModificationException
                    yield();
                }
            }
        };

        Thread remover = new Thread() {
            public void run() {
                for (int i = 0; i < size; i++) {
                    set.remove(new Integer(i));  //删除集合中的元素
                    yield();
                }
            }
        };

        reader.start();
        remover.start();
    }
}